/* eslint-disable array-callback-return */
/* eslint-disable no-param-reassign */
import { message } from 'antd';
import { routerRedux } from 'dva/router';
import {
  getDetExpList,
  delDet,
  getInsUnifyFlag,
  getProjectList,
  getJoinTypeList,
  getDetDetail,
  insertSubmit,
  updateSubmit,
} from '@/services/systemsetting';

export default {
  state: {
    data: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    projectList: [], // 项目列表
    dataSource: [], // 明细表数据源
    tableId: 0, // 表格行标识
    declDetailVo: {},
    submitData: {},
    detail: {
      declDetailCols: [],
      declDetailVo: {},
    },
    joinTypeList: [], // 参保类型
  },
  effects: {
    // 获取申报明细表列表
    *fetchDetExpLList({ payload }, { call, put }) {
      const response = yield call(getDetExpList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      }
    },
    // 删除明细表
    *delDet({ payload, callback }, { call }) {
      const response = yield call(delDet, payload);
      if (callback) callback(response);
    },
    // 项目选项列表
    *fetchProjectList(_, { call, put }) {
      const response = yield call(getProjectList);
      if (response.success && response.data.length) {
        yield put({
          type: 'getProjectList',
          payload: response.data,
        });
      }
    },
    // 项目选项列表
    *fetchProjectLists(payload, { call, put }) {
      const response = yield call(getProjectList, { ...payload.payload });
      if (response.success) {
        if (response.data.length) {
          yield put({
            type: 'getProjectList',
            payload: response.data,
          });
        }
      } else {
        message.warning(response.message || '获取材料信息：服务端错误');
      }
    },
    // 参保类型
    *fetchJoinTypeList(payload, { call, put }) {
      const response = yield call(getJoinTypeList, {
        dictCode: 'ADD_JOIN_TYPE',
        pdId: payload.payload.pdId,
      });
      if (response.success && response.data.length) {
        yield put({
          type: 'getJoinTypeList',
          payload: response.data,
        });
      }
    },
    // 五险合不合一
    *fetchInsUnifyFlag({ payload, callback }, { call }) {
      const response = yield call(getInsUnifyFlag, payload);
      if (response.success && response.data) {
        if (callback) callback(response.data);
      }
    },
    // 提交数据
    *submit({ payload }, { call, put, select }) {
      yield put({
        type: 'getSubmitData',
        payload: payload.declDetailId
          ? { ...payload.values, declDetailId: payload.declDetailId }
          : payload.values,
      });
      const data = yield select(state => state.detexport.submitData);
      const response = yield call(payload.declDetailId ? updateSubmit : insertSubmit, data);
      if (response.success) {
        message.success('保存成功！');
        yield put(
          routerRedux.push({
            pathname: '/systemsetting/detexport/index',
          })
        );
      } else {
        message.warning(`${response.message || '保存失败！'}`);
      }
    },
    // 获取详情数据
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getDetDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    // 获取修改回显数据
    *fetchUpdateInfo({ payload, callback }, { call, put }) {
      const response = yield call(getDetDetail, payload);
      if (response.success) {
        if (callback) callback(response.data);
        yield put({
          type: 'setUpdateInfo',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      list.map(item => {
        // eslint-disable-next-line no-param-reassign
        item.typeName = `${item.type === 1 ? '增员-' : '减员'}${item.subTypeName}`;
        return item;
      });
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取项目列表
    getProjectList(state, { payload }) {
      return {
        ...state,
        projectList: [...payload],
      };
    },
    // 参保身份列表
    getJoinTypeList(state, { payload }) {
      return {
        ...state,
        joinTypeList: [...payload],
      };
    },
    // 获取项目列表
    updateDataSource(state, { payload }) {
      return {
        ...state,
        dataSource: [...payload.dataSource],
        tableId: payload.tableId,
      };
    },
    // 移动行
    onMove(state, { payload }) {
      const { index, nextIndex, dataSource } = payload;
      // eslint-disable-next-line no-shadow
      const moveComm = (arr, curIndex, nextIndex) => {
        // eslint-disable-next-line prefer-destructuring
        arr[curIndex] = arr.splice(nextIndex, 1, arr[curIndex])[0];
        return arr;
      };
      return {
        ...state,
        dataSource: [...moveComm(dataSource, index, nextIndex)],
      };
    },
    // 获取提交数据
    getSubmitData(state, { payload }) {
      // eslint-disable-next-line eqeqeq
      if (payload.insAccu == '2' || !payload.ass) {
        // eslint-disable-next-line no-param-reassign
        payload.ass = payload.insAccu;
      } else {
        // eslint-disable-next-line no-param-reassign
        payload.ass = payload.ass.join(',');
      }
      // eslint-disable-next-line no-param-reassign
      delete payload.insAccu;
      // eslint-disable-next-line no-param-reassign
      payload.subType = payload.subType && payload.subType.join(',');
      let declDetailCols = [];
      if (payload.declDetailCols.length) {
        // eslint-disable-next-line array-callback-return
        state.projectList.map(item => {
          // eslint-disable-next-line array-callback-return
          payload.declDetailCols.map(p => {
            // eslint-disable-next-line eqeqeq
            if (p.matId == item.matId) {
              p.colName = item.colName;
            }
          });
        });
        // eslint-disable-next-line prefer-destructuring
        declDetailCols = payload.declDetailCols;
        delete payload.declDetailCols;
      }
      return {
        ...state,
        submitData: { declDetailCols, declDetailVo: payload },
      };
    },

    // 设置修改回显数据
    setUpdateInfo(state, { payload }) {
      const { declDetailVo } = payload;
      if (declDetailVo.ass) {
        // eslint-disable-next-line eqeqeq
        if (declDetailVo.ass && declDetailVo.ass.indexOf('2') == -1) {
          declDetailVo.insAccu = declDetailVo.ass.split(',');
        }
        if (declDetailVo.type === 1) {
          declDetailVo.subType = declDetailVo.subType && declDetailVo.subType.split(',');
          declDetailVo.subTypeNameList =
            declDetailVo.subTypeName && declDetailVo.subTypeName.split(',');
          declDetailVo.subTypeList = [];
          // eslint-disable-next-line no-unused-expressions
          declDetailVo.subTypeNameList &&
            declDetailVo.subTypeNameList.length &&
            declDetailVo.subTypeNameList.map(item => {
              const newsubType = { itemName: item, itemCode: '' };
              declDetailVo.subTypeList.push(newsubType);
            });
          // eslint-disable-next-line no-unused-expressions
          declDetailVo.subType &&
            declDetailVo.subType.length &&
            declDetailVo.subType.map((itemss, indexss) => {
              if (declDetailVo.subTypeList && declDetailVo.subTypeList.length) {
                declDetailVo.subTypeList[indexss].itemCode = itemss;
              }
            });
        }
      }
      // eslint-disable-next-line array-callback-return
      payload.declDetailCols.map(item => {
        item.key = item.matId || item.key;
      });
      return {
        ...state,
        dataSource: payload.declDetailCols,
        declDetailVo,
      };
    },

    // 获取详情
    getDetail(state, { payload }) {
      return {
        ...state,
        detail: payload,
      };
    },

    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
